package demon5;

import java.util.Arrays;

public class Sequence implements ISequence{
    public int useSize;
    public int[] elem;

    public Sequence(){
        this.elem=new int[10];
    }
    public boolean isFull(){
        return this.elem.length==this.useSize;
    }
    public void expand(){
        this.elem= Arrays.copyOf(this.elem,2*this.elem.length);
    }
    @Override
    public void add(int data) {
        //首先要判断数组是否满,写一个方法
        if(isFull()){
            //如果满了要进行扩容,写一个扩容方法
            expand();
        }
        this.elem[this.useSize]=data;
        this.useSize++;
    }
    public void checkPos(int pos){
        if(pos<0||pos>this.useSize){            // ,这里注意一下顺序表每个元素是顺序存储的
            throw new PosIndexExpection("pos不合法");
        }
    }
    @Override
    public void add(int pos, int data) {
        //这里首先要判断pos是否合法,写一个方法.
        try{
            checkPos(pos);
        }catch (PosIndexExpection e){
            e.printStackTrace();
        }
        if(isFull()){
            expand();
        }
        for (int i = this.useSize-1; i >=pos; i--) {
            this.elem[i+1]=this.elem[i];
        }
        this.elem[pos]=data;
        this.useSize++;
    }

    @Override
    public boolean contains(int toFind) {
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        return 0;
    }

    @Override
    public int get(int pos) {
        return 0;
    }

    @Override
    public void set(int pos, int value) {

    }

    @Override
    public void remove(int toRemove) {

    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public void clear() {

    }

    @Override
    public void display() {

    }
}
